package com.bilibili.service.impl;

import com.bilibili.entity.PicCartoon;
import com.bilibili.entity.PicCategory;
import com.bilibili.entity.PicRegion;
import com.bilibili.exception.BusinessException;
import com.bilibili.mapper.PicCategoryMapper;
import com.bilibili.mapper.PicRegionMapper;
import com.bilibili.service.PicCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

import static com.bilibili.constant.BusinessConstant.CARTOON_MAX_CATEGORY;
import static com.bilibili.constant.BusinessConstant.CARTOON_MAX_REGION;

@Service
public class PicCategoryServiceImpl implements PicCategoryService {
    @Autowired
    private PicCategoryMapper picCategoryMapper;

    @Autowired
    private PicRegionMapper picRegionMapper;


    @Override
    public List<PicCategory> getAllCategory() {
        PicCategory category = picCategoryMapper.getCategoryByName("漫画");
        //拿到漫画的id作为父亲的id
        Integer parentId = category.getId();
        //作为该父亲的id,找到所有与漫画相关的category
        List<PicCategory> categories = picCategoryMapper.getAllCategoryByParentId(parentId);
        PicCategory picCategory = new PicCategory();
        picCategory.setId(CARTOON_MAX_CATEGORY);
        picCategory.setName("全部");
        categories.add(0, picCategory);
        return categories;
    }

    @Override
    public PicCategory getCategoryById(Integer id){
        PicCategory category = picCategoryMapper.getCategoryById(id);
        if (Objects.isNull(category)){
            throw new BusinessException(HttpStatus.BAD_REQUEST, "题材id不存在");
        }
        return category;
    }

    @Override
    public List<PicRegion> getAllRegion() {
        List<PicRegion> regions = picRegionMapper.getAllRegion();
        PicRegion picRegion = new PicRegion();
        picRegion.setId(CARTOON_MAX_REGION);
        picRegion.setRegion("全部");
        regions.add(0, picRegion);
        return regions;
    }
}
